﻿using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindows.Models.Homes;
using UIWindows.Tools.Helper;
using static NPOI.HSSF.Util.HSSFColor;

namespace UIWindows.ViewModels
{
    public class CPKReportFormViewModel : XingLucifer.IBase.IViewStatus
    {
        public GlobalStatics.HomeStatic Home { set; get; }
        private GlobalStatics.SystemConfigStatic _configStatic;
        private GlobalStatics.ParameterStatic _parameter;
        public CPKReportFormViewModel(GlobalStatics.SystemConfigStatic configStatic, GlobalStatics.HomeStatic home, GlobalStatics.ParameterStatic parameter)
        {
            _configStatic = configStatic;
            Series = new System.Collections.ObjectModel.ObservableCollection<ISeries>();
            StationNoList = new System.Collections.ObjectModel.ObservableCollection<string>();
            Home = home;
            _parameter = parameter;
            UpperLimit = new ObservableCollection<double>();
            StandardValue = new ObservableCollection<double>();
            LowerLimit = new ObservableCollection<double>();
        }
        public System.Collections.ObjectModel.ObservableCollection<string> StationNoList { get; set; }
        public System.Collections.ObjectModel.ObservableCollection<ISeries> Series { get; set; }
        private string _selectedStationNo;

        public string SelectedStationNo
        {
            get { return _selectedStationNo; }
            set
            {
                _selectedStationNo = value;
                IsFilterDisplay(value);
            }
        }

        private void IsFilterDisplay(string value)
        {
            foreach (var item in Series.Where(x => x.Name != "Upper limit" && x.Name != "Standard" && x.Name != "Lower limit"))
            {
                item.IsVisible = false;
                item.IsVisibleAtLegend = false;
            }
            if (value == "全部")
            {
                for (int i = 0; i < 7; i++)
                {
                    Series[i].IsVisible = true;
                    Series[i].IsVisibleAtLegend = true;
                }
                return;
            }
            var model = Series.FirstOrDefault(x => x.Name == value);
            if (model != null)
            {
                model.IsVisible = true;
                model.IsVisibleAtLegend = true;
            }
        }
        private SkiaSharp.SKColor[] sKColors = new SkiaSharp.SKColor[] { SkiaSharp.SKColors.Gold,
            SkiaSharp.SKColors.Black, SkiaSharp.SKColors.Teal, SkiaSharp.SKColors.Aqua,SkiaSharp.SKColors.CadetBlue, SkiaSharp.SKColors.Chocolate, SkiaSharp.SKColors.DarkMagenta,
        SkiaSharp.SKColors.DarkGoldenrod,SkiaSharp.SKColors.DarkGreen,SkiaSharp.SKColors.DarkSalmon};

        public Axis[] XAxes { get; set; } =
        {
            new Axis
            {
                MinLimit = 1,
                MaxLimit = 50,
                ForceStepToMin = true,
                MinStep = 1,
                TextSize = 14,
                //SeparatorsPaint = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint
                //{
                //    Color = SkiaSharp.SKColors.Aqua,
                //    StrokeThickness = 2
                //}
            }
        };

        public List<Axis> YAxes { get; set; } = new List<Axis>
        {
            new Axis
            {
                ForceStepToMin = true,
                MinStep = 0.5,
                SeparatorsPaint = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint
                {
                    Color = SkiaSharp.SKColors.DarkGray,
                    StrokeThickness = 1
                }
            }
        };
        SkiaSharp.SKColor _red = new SkiaSharp.SKColor(229, 57, 53);
        SkiaSharp.SKColor _gree = new SkiaSharp.SKColor(0x00, 0xEC, 0x00);
        /// <summary>
        /// 上限
        /// </summary>
        private ObservableCollection<double> UpperLimit { get; set; }
        /// <summary>
        /// 标准值
        /// </summary>
        private ObservableCollection<double> StandardValue { get; set; }
        /// <summary>
        /// 下限
        /// </summary>
        private ObservableCollection<double> LowerLimit { get; set; }
        private void AddSeries()
        {
            Series.Clear();
            StationNoList.Clear();
            StationNoList.Add("全部");
            SolidColorPaint rad = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint(_red, 2);
            var _lineSeries = new LineSeries<double>
            {
                Values = UpperLimit,
                Stroke = rad,
                GeometryStroke = rad,
                LineSmoothness = 0,
                GeometrySize = 8,
                Fill = null,
                Name = "Upper limit",
            };
            var _lineSeries1 = new LineSeries<double>
            {
                Values = StandardValue,
                Stroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint(_gree, 2),
                GeometryStroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint(_gree, 2),
                LineSmoothness = 0,
                GeometrySize = 8,
                Fill = null,
                Name = "Standard",
            };
            var _lineSeries2 = new LineSeries<double>
            {
                Values = LowerLimit,
                Stroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint(_red, 2),
                GeometryStroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint(_red, 2),
                LineSmoothness = 0,
                GeometrySize = 8,
                Fill = null,
                Name = "Lower limit",
                
            };
            Series.Add(_lineSeries);
            Series.Add(_lineSeries1);
            Series.Add(_lineSeries2);
            try
            {
                foreach (var item in Home.Trend)
                {
                    StationNoList.Add(item.Name);
                    Series.Add(new LineSeries<double>
                    {
                        Values = item.InjectionVolumeCollection,
                        IsVisible = Series.Count < 7,
                        GeometryStroke = null,
                        GeometryFill = null,
                        Stroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint
                        {
                            Color = sKColors[new Random().Next(0, sKColors.Length)],
                            StrokeCap = SkiaSharp.SKStrokeCap.Round,
                            StrokeThickness = 1,
                        },
                        LineSmoothness = 0,
                        GeometrySize = 0,
                        Fill = null,
                        Name = item.Name,
                    });
                }
            }
            catch (Exception ex)
            {
                $"CPK 界面加载异常：{ex}".RunLog(XingLucifer.IBase.Enums.MessageLevelType.警告);
            }
            //Home.Trend.Clear();
            //for (int k = 0; k < _configStatic.SCM.PumpNumber; k++)
            //{
            //    for (int i = 0; i < _configStatic.SCM.RowsNumber; i++)
            //    {
            //        for (int j = 0; j < _configStatic.SCM.ColumnsPumps; j++)
            //        {
            //            var model = new TrendModel()
            //            {
            //                Name = $"{k + 1}_{i + 1}_{j + 1}",
            //                InjectionVolumeCollection = new System.Collections.ObjectModel.ObservableCollection<double>(),
            //                ProcessCapability = new ProcessCapabilityModel(),
            //            };
            //            StationNoList.Add(model.Name);
            //            Home.Trend.Add(model);
            //            Series.Add(new LineSeries<double>
            //            {
            //                Values = model.InjectionVolumeCollection,
            //                IsVisible = Series.Count < 7,
            //                GeometryStroke = null,
            //                GeometryFill = null,
            //                Stroke = new LiveChartsCore.SkiaSharpView.Painting.SolidColorPaint
            //                {
            //                    Color = sKColors[new Random().Next(0, sKColors.Length)],
            //                    StrokeCap = SkiaSharp.SKStrokeCap.Round,
            //                    StrokeThickness = 1,
            //                },
            //                LineSmoothness = 0,
            //                GeometrySize = 0,
            //                Fill = null,
            //                Name = model.Name,
            //            });
            //        }
            //    }
            //}
        }

        public void Load()
        {
            UpperLimit.Clear();
            StandardValue.Clear();
            LowerLimit.Clear();
            for (int i = 0; i < 50; i++)
            {
                UpperLimit.Add(_parameter.RangeParameter.QuantitativeUp);
                StandardValue.Add(_parameter.RangeParameter.Quantitative);
                LowerLimit.Add(_parameter.RangeParameter.QuantitativeDown);
            }
            YAxes[0].MaxLimit = _parameter.RangeParameter.QuantitativeUp + 2;
            YAxes[0].MinLimit = _parameter.RangeParameter.QuantitativeDown - 2;

            AddSeries();
        }

        public void UnLoad()
        {

        }
    }
}
